package cn.xq.pethome.shop.controller;

import cn.xq.pethome.shop.domain.ShopAuditLog;
import cn.xq.pethome.shop.service.IShopService;
import cn.xq.pethome.shop.domain.Shop;
import cn.xq.pethome.shop.query.ShopQuery;
import cn.xq.pethome.utils.AjaxUtil;
import cn.xq.pethome.utils.BusinessException;
import cn.xq.pethome.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxUtil转换结果
     */
    @PutMapping
    public AjaxUtil addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null){
                    shopService.update(shop);
                return AjaxUtil.success().setMsg("修改成功");
            }else{
                shopService.insert(shop);
                return AjaxUtil.success().setMsg("添加成功");}
        } catch (BusinessException a){
            a.printStackTrace();
            return AjaxUtil.error().setMsg("输入错误！");
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxUtil.error().setSuccess(false).setMsg("网络异常，请稍后再试！");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxUtil delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxUtil.error().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.fingone(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.fingall();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pagelist")
    public AjaxUtil json(@RequestBody ShopQuery query)
    {
        List<Shop> findquery = shopService.findquery(query);
        return AjaxUtil.success().setData(findquery);
    }

    @PostMapping("/audit/pass")
    public AjaxUtil  auditpass(@RequestBody ShopAuditLog shopAuditLog){
         shopService.pass(shopAuditLog);
        return AjaxUtil.success().setMsg("申请通过！");
    }
    @PostMapping("audit/reject")
    public AjaxUtil auditreject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.reject(shopAuditLog);
        return AjaxUtil.success().setMsg("申请驳回！");
    }
    @GetMapping("/export")
    public AjaxUtil export(HttpServletResponse response){
        List<Shop> fingall = shopService.fingall();
        ExcelUtil.exportExcel(fingall,"shop","店铺信息",Shop.class,"shop.xls",response);
        return AjaxUtil.success();
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");

    }

}
